package com.hdu.pyl.util;

import Jama.Matrix;
import com.hdu.pyl.pojo.NewsTags;
import com.hdu.pyl.pojo.UserTags;
import org.apache.commons.math3.linear.ArrayRealVector;
import org.apache.commons.math3.linear.RealMatrix;
import org.apache.commons.math3.linear.RealVector;
import org.apache.mahout.math.Vector;

import java.util.Arrays;


public class MathUtil {
    /**
     * 计算标签相似度
     * @param userTags
     * @param newsTags
     * @return
     */
    public static double userTagsTimeNewsTags(UserTags userTags, NewsTags newsTags) {
        double[] user = getTagsDouble(userTags);
        double[] news = getTagsDouble(newsTags);
        RealVector u = new ArrayRealVector(user);
        RealVector n = new ArrayRealVector(news);
        return sum(u.ebeMultiply(n).toArray());
    }

    private static double sum(double[] doubles) {
        if (doubles == null) {
            return 0;
        }
        double s = 0;
        for (int i = 0; i < doubles.length; i++) {
            s += doubles[i];
        }
        return s;
    }

    /**
     * userTags与newsTags的double[]顺序是
     * 0.weightUp、1.weightDown、2.heightUp、3.heightDown、4.headUp、5.headDown、6.bmiUp、7.bmiDown、8.bodyFatUp、9.bodyFatDown
     */
    private static double[] getTagsDouble(UserTags tags) {
        double[] doubles = new double[10];
        doubles[0] = tags.getWeightUp();
        doubles[1] = tags.getWeightDown();
        doubles[2] = tags.getHeightUp();
        doubles[3] = tags.getHeightDown();
        doubles[4] = tags.getHeadUp();
        doubles[5] = tags.getHeadDown();
        doubles[6] = tags.getBmiUp();
        doubles[7] = tags.getBmiDown();
        doubles[8] = tags.getBodyFatUp();
        doubles[9] = tags.getBmiDown();
        return doubles;
    }

    private static double[] getTagsDouble(NewsTags tags) {
        double[] doubles = new double[10];
        doubles[0] = tags.getWeightUp();
        doubles[1] = tags.getWeightDown();
        doubles[2] = tags.getHeightUp();
        doubles[3] = tags.getHeightDown();
        doubles[4] = tags.getHeadUp();
        doubles[5] = tags.getHeadDown();
        doubles[6] = tags.getBmiUp();
        doubles[7] = tags.getBmiDown();
        doubles[8] = tags.getBodyFatUp();
        doubles[9] = tags.getBmiDown();
        return doubles;
    }

    public static void main(String[] args) {
        double[] a = new double[]{2d, 2d, 3d};
        double[] b = new double[]{0.8, 0.6, 0.1};
        RealVector r1 = new ArrayRealVector(a);
        RealVector r2 = new ArrayRealVector(b);
        System.out.println(Arrays.toString(r1.ebeMultiply(r2).toArray()));
    }

    /**
     * Sigmoid函数
     * @param input
     * @return
     */
    public static double Sigmoid(double input) {
        return 1 / (1 + Math.exp(-input));
    }
}
